/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.model
{
	import multimedia.control.event.feed.GetFeedEvent;
	import multimedia.control.event.media.GetMediaEvent;
	import multimedia.control.event.media.GetUserMediaEvent;
	import multimedia.control.event.playlist.GetPlaylistsEvent;
	import multimedia.control.event.user.GetUserEvent;
	import multimedia.control.event.user.GetUserGroupsEvent;
	import multimedia.model.vo.UserVO;
	
	import mx.controls.Alert;

	[Bindable]
	public class LoginModel
	{
		private static var instance:LoginModel;

		public static const EDITOR:uint = 1;
		public static const AUTHOR:uint = 2;
		public static const USERMANAGEMENT:uint = 3;
		public static const ROOTEDITOR:uint = 4;

		public static function getInstance():LoginModel
		{
			if (instance == null)
				instance = new LoginModel();
			return instance;
		}

		public function LoginModel()
		{
			if (instance != null)
			{
				throw new Error( "LoginModel. Only one LoginModel instance should be instantiated");
			}
		}

		private var _user:UserVO;

		public function get user():UserVO
		{
			return _user;
		}

		public function set user(user:UserVO):void
		{
			this._user = user;
			var viewLocator:ViewLocator = ViewLocator.getInstance();
			switch (this._user.user_group.group_id)
			{
				case AUTHOR:
					viewLocator.view.selectedChild = viewLocator.authorView;
					new GetUserMediaEvent(user).dispatch();
					break;
				case EDITOR:
					viewLocator.view.selectedChild = viewLocator.editorView;
					new GetMediaEvent(false).dispatch();
					new GetMediaEvent(true).dispatch();
					new GetFeedEvent().dispatch();
					new GetPlaylistsEvent(user).dispatch();
					new GetPlaylistsEvent().dispatch();
					break;
				case USERMANAGEMENT:
					viewLocator.view.selectedChild = viewLocator.usermanagementView;
					new GetUserEvent().dispatch();
					new GetUserGroupsEvent().dispatch();
					new GetPlaylistsEvent().dispatch();
					break;
				case ROOTEDITOR:
					viewLocator.view.selectedChild = viewLocator.editorView;
					new GetMediaEvent(false).dispatch();
					new GetMediaEvent(true).dispatch();
					new GetFeedEvent().dispatch();
					new GetPlaylistsEvent(user).dispatch();
					new GetPlaylistsEvent().dispatch();
					break;
				default:
					ViewLocator.getInstance().view.selectedIndex = 0;
					Alert.show("Authentifizierung fehlgeschlagen", "Login");
					break;
			}
		}

		public function logout():void
		{
			_user = new UserVO();
			ViewLocator.getInstance().view.selectedIndex = 0;
			ViewLocator.getInstance().loginView.clearInput();
		}

	}
}